﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Kamikaze
{
    class Personagem : AnimatedObject
    {
       // public SoundEffect Effect; 

        float gravidade;
        float velocidadeY;

        KeyboardState teclado;
        KeyboardState tecladoAntigo;

        bool podePular;

        int Height;

        int Width;


        public Personagem(Texture2D texture, int Width, int Height)


            : base(texture, Width, Height)

         
        {
            AnimationStructure animationCorrendo = new AnimationStructure();
            animationCorrendo.frameCount = 5;
            animationCorrendo.frameWidth = Width;
            animationCorrendo.frameHeight = Height;
            animationCorrendo.framePerSecond = 12;
            animationCorrendo.framePerSecond = 6;
            animationCorrendo.X = 0;
            animationCorrendo.Y = 0;
            listaAnimations.Add("correndo", animationCorrendo);

            

            AnimationStructure animationPulando = new AnimationStructure();
            animationPulando.frameCount = 1;
            animationPulando.frameWidth = Width;
            animationPulando.frameHeight = Height;
            animationPulando.framePerSecond = 10;
            animationPulando.X = Width * 5;
            animationPulando.Y = 0;
            listaAnimations.Add("pulando", animationPulando);


            
            gravidade = 0.1f;
            velocidadeY = -1;
          
            podePular = false;

            this.Width = Width;
            this.Height = Height;
        }


        public override void Update(GameTime gametime) 
        {
            teclado = Keyboard.GetState();
           

            
            if (podePular == true)
            {
                
                velocidadeY += gravidade;
                posicao.Y += velocidadeY;

                //Console.WriteLine(posicao.Y + " " + posicaoChao.Y);

                if (posicao.Y > posicaoChao.Y)
                {
                    posicao.Y = (posicaoChao.Y) - (texture.Height / 2);
                    velocidadeY = 0;
                    podePular = false;

                    posicao.Y = posicaoChao.Y;
                    //effect.Play();
                }

            }
            if(podePular==false)
            {
                ChangeAnimation("correndo");
            }

           

            if (Keyboard.GetState().IsKeyDown(Keys.Up) && posicao.Y >= 280 - (Height / 2) && podePular == false)
           {
               posicao.Y -= 6;
               posicaoChao.Y = posicao.Y;
           }

           if (Keyboard.GetState().IsKeyDown(Keys.Down) && posicao.Y < 400 - (Height / 2) && podePular == false)
           {
               posicao.Y += 6;
               posicaoChao.Y = posicao.Y;
           }

           if (Keyboard.GetState().IsKeyDown(Keys.Space) && tecladoAntigo.IsKeyUp(Keys.Space) && posicao.Y == posicaoChao.Y)
           {
               podePular = true;
               velocidadeY = -5;

               ChangeAnimation("pulando");
               //effect.Play(); 

          
           }
        

           tecladoAntigo = teclado;
           base.Update(gametime);


        }

        }
    }

